package com.http.utils;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: JsonUtil
 * @Description: JSON工具类
 * @author: QT
 * @date: 2020年10月09日 上午9:26:37
 */
public class JsonUtil {

    private static Logger logger = LoggerFactory.getLogger(JsonUtil.class);

    /**
     * 将JSON字符串转为对象
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T json2Object(String json, Class<T> clazz) {
        try {
            return JSON.parseObject(json, clazz);
        } catch (Exception e) {
            logger.error("Throws Exception when cast Json String to Object...\n" + e.getMessage());
        }
        return null;
    }

    /**
     * 将JSON对象转为String字符串
     * @param obj
     * @return
     */
    public static String object2Json(Object obj) {
        return JSON.toJSONString(obj);
    }

}
